﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;
using SIS.UI;

public partial class school_location : System.Web.UI.Page
{
    Common _common = new Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }

            _common.LoadCountryCodeDDL(ddlCountry, string.Empty);
        }
    }

    //private void LoadSchoolList()
    //{
    //    ddlSchoolList.Items.Clear();
    //    SchoolProfileTableList _profileList = SchoolProfileTableManager.GetList();
    //    if (_profileList != null)
    //    {
    //        foreach (SchoolProfileTable _profile in _profileList)
    //        {
    //            ddlSchoolList.Items.Add(new ListItem(_profile.SchoolName, _profile.SchoolKey.ToString()));
    //        }
    //    }
    //}

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            try
            {
                LocationTable _Location = new LocationTable();
                _Location.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
                _Location.LocationKey = Convert.ToInt32(lblLocationKey.Text);
                _Location.LocationCode = txtLocationCode.Text;
                _Location.LocationName = txtLocationName.Text;
                _Location.ContactPersonKey = 1;
                _Location.ContactNo = txtContactNo.Text;
                _Location.FaxNo = txtFaxNo.Text;
                _Location.EmailAddress = txtEmailAddress.Text;
                _Location.WebsiteUrl = txtWebsiteUrl.Text;
                _Location.Address1 = txtAddress1.Text;
                _Location.Address2 = txtAddress2.Text;
                _Location.StreetName = txtStreetName.Text;
                //_Location.TownshipCode = Convert.ToInt16(ddlTownship.Text);
                _Location.CityCode = Convert.ToInt16(ddlCity.Text);
                _Location.DistrictCode = Convert.ToInt16(ddlDistrict.Text);
                _Location.CountryCode = Convert.ToInt16(ddlCountry.Text);
                //_Location.AddressTypeCode = Convert.ToInt16(ddlAddressType.Text);
                _Location.PostalCode = txtPostalCode.Text;
                _Location.Status = 1;
                int _LocationKey = LocationTableManager.Save(_Location);
                if (_LocationKey > 0)
                {
                    ClearFields();
                    grvLocation.DataBind();
                    lblMessage.Text = "Saved sussccessfully!";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = "Saving failed!<br/>" + Ex.Message ;
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }


    protected void btnShowLocation_Click(object sender, EventArgs e)
    {

    }


    protected void grvLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        if (grvLocation.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvLocation.SelectedDataKey;
            int _LocationKey = Convert.ToInt32(_selectedDataKey[0]);
            int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);

            if (_LocationKey > 0 && _schoolKey > 0)
            {

                //GridViewRow _selectedRow = grvLocation.SelectedRow;
                //txtLocationCode.Text = _selectedRow.Cells[2].Text;
                //txtLocationName.Text = _selectedRow.Cells[3].Text;
                //lblMessage.Text = _selectedRow.Cells[4].Text;
                //CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[4].Controls[0]);
                //chkStatus.Checked = _chkStatus.Checked;
                //lblMessage.Text = _chkStatus.Checked.ToString();
                LocationTable _location = LocationTableManager.GetItem(_LocationKey);
                if (_location != null)
                {
                    lblSchoolKey.Text = _location.SchoolKey.ToString();
                    lblLocationKey.Text = _location.LocationKey.ToString();
                    txtLocationCode.Text = _location.LocationCode;
                    txtLocationName.Text = _location.LocationName;
                    txtContactNo.Text = _location.ContactNo;
                    txtFaxNo.Text = _location.FaxNo;
                    txtEmailAddress.Text = _location.EmailAddress;
                    txtWebsiteUrl.Text = _location.WebsiteUrl;
                    txtAddress1.Text = _location.Address1;
                    txtAddress2.Text = _location.Address2;
                    txtStreetName.Text = _location.StreetName;
                    //ddlTownship.Text = _location.TownshipCode.ToString();
                    ddlCountry.Text = _location.CountryCode.ToString();
                    ddlCountry_SelectedIndexChanged(sender, e);
                    if (ddlDistrict.Items.Count > 0)
                    {
                        ddlDistrict.Text = _location.DistrictCode.ToString();
                        ddlDistrict_SelectedIndexChanged(sender, e);

                        if (ddlCity.Items.Count > 0)
                        {
                            ddlCity.Text = _location.CityCode.ToString();
                        }
                    }
                    
                    //ddlAddressType.Text = _location.AddressTypeCode.ToString();
                    txtPostalCode.Text = _location.PostalCode;
                }
            }
        }
    }

    private void ClearFields()
    {
        lblMessage.Text = "";
        lblSchoolKey.Text = ddlSchoolList.Text;
        lblLocationKey.Text = "-1";
        txtLocationCode.Text = "";
        txtLocationName.Text = "";
        txtContactNo.Text = "";
        txtFaxNo.Text = "";
        txtEmailAddress.Text = "";
        txtWebsiteUrl.Text = "";
        txtAddress1.Text = "";
        txtAddress2.Text = "";
        txtStreetName.Text = "";
        ddlTownship.Text = "0";
        if (ddlCountry.Items.Count > 0)
        {
            ddlCountry.SelectedIndex = 0;
            ddlCountry_SelectedIndexChanged(null, null);

            if(ddlDistrict.Items.Count > 0)
            {
                ddlDistrict.SelectedIndex = 0;
                ddlDistrict_SelectedIndexChanged(null,null);
            }

            if (ddlCity.Items.Count > 0)
            {
                ddlCity.SelectedIndex = 0;
            }

        }
        
        ddlAddressType.Text = "0";
        txtPostalCode.Text = "";
        //chkStatus.Checked = false;
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblLocationKey.Text.Length > 0)
        {
            string errorMessage = string.Empty;
            if (LocationTableManager.Delete(Convert.ToInt32(lblLocationKey.Text), out errorMessage))
            {
                ClearFields();
                grvLocation.DataBind();
                lblMessage.Text = "Item was deleted!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Deleting failed!<br/>" + errorMessage;
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please select an item to delete.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlDistrict, Convert.ToInt16(ddlCountry.Text), string.Empty);
        if (ddlDistrict.Items.Count > 0)
        {
            ddlDistrict.SelectedIndex = 0;
            ddlDistrict_SelectedIndexChanged(sender, e);
        }
    }
    protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadCityCodeDDL(ddlCity, Convert.ToInt32(ddlDistrict.Text), Convert.ToInt16(ddlCountry.Text), string.Empty);
    }
}
